{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "  #                                     控制语句实例\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一. 条件语句 if的用法：if...else..., if...elif...elif..else..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a number:-8\n",
      "The number is negative\n"
     ]
    }
   ],
   "source": [
    "#条件语句的例子\n",
    "num=input('Enter a number:' )\n",
    "if float(num)>0:\n",
    "    print('The number is positive')\n",
    "elif float(num)<0:\n",
    "    print('The number is negative')\n",
    "else:\n",
    "    print('The number is zero')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter hours you spend on Python study: 32\n",
      "Advise you to spend more time on Mathematics\n"
     ]
    }
   ],
   "source": [
    "#条件语句嵌套\n",
    "hours=input(\"Enter hours you spend on Python study: \")\n",
    "hours=int(hours)\n",
    "if hours>=1:\n",
    "    if hours<=2:\n",
    "        print(\"You need to study harder,unless you have been an expert at programming\")\n",
    "    elif 2<hours<5:\n",
    "        print(\"You will qualify this course\")\n",
    "    else:   \n",
    "        print(\"Advise you to spend more time on Mathematics\")\n",
    "else:\n",
    "    print(\"You have to choose this course again next year!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二. 循环语句while与for用法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0* 1* 2* 3* 4* 5* 6* 7* 8* 9* 10* "
     ]
    }
   ],
   "source": [
    "i=0\n",
    "while i<=10:\n",
    "      print(\"{}*\".format(i),end=' ')\n",
    "      i+=1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## while True句式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please enter a word: tertr\n",
      "The word wastertr\n",
      "Please enter a word: terrr\n",
      "The word wasterrr\n",
      "Please enter a word: err\n",
      "The word waserr\n",
      "Please enter a word: Leo\n"
     ]
    }
   ],
   "source": [
    "#while True 与break结合用的例子。当条件为真时永远执行。\n",
    "while True:\n",
    "    word=input('Please enter a word: ')\n",
    "    if  'L' in word:\n",
    "        break\n",
    "    print('The word was'+word)    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 句式：while break,continue 或 pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 4 6 8 10 "
     ]
    }
   ],
   "source": [
    "#while break 中止循环；while continue 跳出当前步进入下次迭代；while pass 继续执行下面命令。\n",
    "\n",
    "\n",
    "i=0\n",
    "while i<=10:\n",
    "    i+=1\n",
    "    if i%2==1:\n",
    "            continue #pass,break\n",
    "    print(i,end=' ')      \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 句式：while else"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3 4 5 第一个超出5的整数：6\n"
     ]
    }
   ],
   "source": [
    "#while...else..当while条件时False时执行else语句。\n",
    "count=1\n",
    "while count<=5:\n",
    "    print(count,end=' ')\n",
    "    count+=1\n",
    "else:\n",
    "    print(\"第一个超出5的整数：{}\".format(count))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for 执行一般序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]\n"
     ]
    }
   ],
   "source": [
    "# for 简单循环例子：\n",
    "for num in range(1,10):\n",
    "    print(num)\n",
    "    \n",
    "import numpy as np # 可以以小数间隔生成序列\n",
    "r=np.arange(0,1,0.1)\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "anne is 12 years old\n",
      "beth is 18 years old\n",
      "george is 17 years old\n",
      "damon is 16 years old\n"
     ]
    }
   ],
   "source": [
    "#for实现并行迭代\n",
    "names=['anne','beth','george','damon']\n",
    "ages=[12,18,17,16]\n",
    "for i in range(len(names)):\n",
    "    print(names[i],'is',ages[i],'years old')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 内置函数zip的功能，等价于循环语句。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('anne', 12), ('beth', 18), ('george', 17), ('damon', 16)]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用内置的函数zip可以实现并行迭代，把多个序列“压缩一起”\n",
    "names=['anne','beth','george','damon']\n",
    "ages=[12,18,17,16]\n",
    "list(zip(names,ages))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<zip at 0x24832b29708>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "zip(range(5),range(10000))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### map函数的功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25]\n"
     ]
    }
   ],
   "source": [
    "#map会根据提供的函数对指定序列做映射\n",
    "def square(x):\n",
    "    return(x*x)\n",
    "x=map(square,[1,2,3,4,5])\n",
    "print(list(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 7, 11, 15, 19]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 句式 for ....break...,for...continue...,for....pass..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前字母 : P\n",
      "当前字母 : y\n",
      "当前字母 : t\n"
     ]
    }
   ],
   "source": [
    "#break例子：\n",
    "for letter in 'Python':     # 第一个实例\n",
    "    if letter == 'h':\n",
    "        break\n",
    "    print('当前字母 :', letter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前字母 : P\n",
      "当前字母 : y\n",
      "当前字母 : t\n",
      "当前字母 : o\n",
      "当前字母 : n\n"
     ]
    }
   ],
   "source": [
    "#continue 例子\n",
    "for letter in 'Python':     # 第一个实例\n",
    "    if letter == 'h':\n",
    "        continue\n",
    "    print('当前字母 :', letter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是 pass 块\n",
      "当前字母 : P\n",
      "这是 pass 块\n",
      "当前字母 : y\n",
      "这是 pass 块\n",
      "当前字母 : t\n",
      "这是 pass 块\n",
      "当前字母 : h\n",
      "这是 pass 块\n",
      "当前字母 : o\n",
      "这是 pass 块\n",
      "当前字母 : n\n",
      "Good bye!\n"
     ]
    }
   ],
   "source": [
    "# Pass的功能：输出 Python 的每个字母\n",
    "for letter in 'Python':\n",
    "    if letter == 'h':\n",
    "        pass\n",
    "    print ('这是 pass 块')\n",
    "    print('当前字母 :', letter)\n",
    "\n",
    "print (\"Good bye!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for else句式的用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 "
     ]
    }
   ],
   "source": [
    "list1=[1,2,-3,4,5]\n",
    "for l in list1:\n",
    "    if l<0:\n",
    "        break\n",
    "    print(l,end=' ')\n",
    "else:\n",
    "    print(\"All the numbers have been printed\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#当for语句中没有执行break的话，遍历完for语句，就会执行else语句\n",
    "#但是如果中间执行了break语句，跳出for循环，那么不会执行else语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
